package com.ihighest.app.common.kit;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.annotation.SuppressLint;

/**
 * DES 加密、解密工具
 * 
 * @author Vity
 * @date 2014年11月6日
 * @mail vity.1215@gmail.com
 */
public class DESKit {

	final static String DES = "DES";

	final static String DES_KEY = "ihighest";

	private static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };

	@SuppressLint("TrulyRandom")
	public static String encryptDES(String encryptString, String encryptKey) throws Exception {
		IvParameterSpec zeroIv = new IvParameterSpec(iv);
		SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), DES);
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
		byte[] encryptedData = cipher.doFinal(encryptString.getBytes());

		return Base64Kit.encode(encryptedData);
	}

	public static String decryptDES(String decryptString, String decryptKey) throws Exception {
		byte[] byteMi = Base64Kit.decode(decryptString);
		IvParameterSpec zeroIv = new IvParameterSpec(iv);
		SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), DES);
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
		byte decryptedData[] = cipher.doFinal(byteMi);

		return new String(decryptedData);
	}

	public static String encryptDES(String encryptString) throws Exception {
		return encryptDES(encryptString, DES_KEY);
	}

	public static String decryptDES(String decryptString) throws Exception {
		return decryptDES(decryptString, DES_KEY);
	}
}